﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class BM1 : BasePage
{

    string emId = string.Empty;
    string emYear = string.Empty;
    string emNum = string.Empty;
    string orderId = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (Request.QueryString.Count > 0)
        {
            emYear = Request.Params["EmYear"].Trim();
            emNum = Request.Params["EmNum"].Trim();
            emId = Request.Params["EmID"].Trim();
        }
        else
        {
            Response.Redirect(BSUtility.mainUrl + "default.aspx");
        }
        NoAccess.JumpToDefaultPageNoAccessTime(this, emId);
        //Session["emId"] = emId; 
        DataTable dt_apply = Exam.getBMInfoByStudIdCard(IDCard, emId, emYear, emNum);
        if (dt_apply.Rows.Count > 0)
        {
            DataRow dr_apply = dt_apply.Rows[0];
            orderId = dr_apply["OrderID"].ToString();
            int applyState = int.Parse(dr_apply["ApplyState"].ToString());
            if (applyState == 1)
            {
                Response.Redirect("BM4.aspx?orderID=" + orderId);
            }
            else if (applyState == 2)
            {
                Response.Redirect("BM5.aspx?orderID=" + orderId);
            }
        }


        if (!Page.IsPostBack)
        {
            DataTable dt = StudInfo_Control.getStudInfoByStudIdCard(Session["studIdcard"].ToString());
            if (dt.Rows.Count == 1)
            {
                DataRow dr = dt.Rows[0];
                NameText.Text = dr["StudName"].ToString();
                SFZHText.Text = dr["StudIdcard"].ToString();
                if (dr["StudSex"].ToString().Trim() == "女")
                {
                    rbMan.Checked = false;
                    rbMale.Checked = true;
                }
                SIDText.Text = dr["StudID"].ToString();
                ClassText.Text = dr["StudClassName"].ToString();
                DepText.Text = dr["StudDeptName"].ToString();
                FromText.Text = dr["StudFrom"].ToString();
                tbPhone.Text = dr["StudTel"].ToString();
                userPic.ImageUrl = BSUtility.mainUrl + "StudPhoto/" + IDCard.Trim() + ".jpg";
            }
            else
            {
                NameText.Text = Session["userName"].ToString();
                SFZHText.Text = Session["studIdcard"].ToString();
            }
        }
    }

    protected override void Page_LoadAlways()
    {
        base.Page_LoadAlways();
        DataTable dtStudInfo = StudInfo_Control.getStudInfoByStudIdCard(IDCard);
        if (dtStudInfo.Rows.Count > 0)
        {
            //确认BM信息的状态，如果未确认，则转入BM1页面，BM1、BM2、BM3为一系列，
            string BM1Page = string.Empty;
            string BM2Page = string.Empty;
            string BM3Page = string.Empty;

            if (dtStudInfo.Rows[0]["StudState"].ToString() == "0" && (Request.Url.AbsoluteUri == BM1Page || Request.Url.AbsoluteUri == BM2Page || Request.Url.AbsoluteUri == BM3Page))
            {
                //跳转到BM1页面，上述语句的意思是当状态为“未确认”且当前页面不属于BM1、2、3中的任意一个时，跳转回BM1，但是类型怎么办？同一时间有多场考试？怎么办？
                //因为考试带有类型，没有办法，所以跳转回考试选择页面
                Response.Redirect(BSUtility.mainUrl + "BM/ExamSelect.aspx");
                
            }
        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        string studSex = (rbMan.Checked == true) ? "男" : "女";
        DataTable dt = StudInfo_Control.getStudInfoByStudIdCard(IDCard);
        if (dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];
            string userPic = dr["StudPic"].ToString();
            string[] studInfo = new string[] { IDCard, UserName.Trim(), studSex, DepText.Text.Trim(), ClassText.Text.Trim(), userPic, "0", SIDText.Text.Trim(), FromText.Text.Trim(), tbPhone.Text.Trim() };
            if (StudInfo_Control.StudInfo_Save(studInfo, IDCard))
            {
                Response.Redirect("BM2.aspx?EmYear=" + emYear + "&EmNum=" + emNum+"&EmID="+emId);
            }
            else
            {
                BSUtility.WindowAlert(this, "抱歉，请重试!");
            }
        }

        
        
    }
}
